// 2013-7-29下午07:37:35  net.betou.core.service.impl.NoticePostDataListener.java

package net.betou.core.service.impl;

import java.util.Map;

import net.betou.common.util.DateUtils;
import net.betou.core.service.PostDataAbstract;
import net.betou.core.service.post.PostKeyVal;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Notice;

import org.apache.log4j.Logger;
import org.jsoup.Connection.KeyVal;
import org.springframework.stereotype.Component;

/**
 * 使用俄文“ ф”字符来分隔
 * 发布的格式：gonggaoф1ф2ф0ф11ф5,6,7ф这是我的一切ф我的内容，我的一切ф2013-08-11
 * 11:22:33фwww.a.com:8080/view.jhtml?id=3ф
 * 
 * @author 叶装装
 */
@Component
public class NoticePostDataListener extends PostDataAbstract< Notice >{

	private static final Logger log = Logger.getLogger( NoticePostDataListener.class );

	
	
	@Override
	public void afterChange( Notice bean, Map< String, Object > map ) {
		
		AgentAndAdmins admin = bean.getAdmin( );
		
		if( admin.getSystem( ) ){
			return;
		}
		send( admin, bean );
	}



	/**
	 */
	public void afterSave( final Notice bean ) {
		
		final AgentAndAdmins admin = bean.getAdmin( );
		
		// 是系统公告，不发送
		if( admin.getSystem( ) ){
			return;
		}
		
		KeyVal key = new PostKeyVal( mng ){
			@Override
			public String processValue( ) {
				
				StringBuffer buffer = new StringBuffer( );
				
				boolean agency = admin.getAgency( );
				
				// 发布标识
				this.appendToBuffer( buffer, "gonggao" );
				// 发布单位
				this.appendToBuffer( buffer, agency ? 1 : 0 );
				
				Integer [ ] agencyProxy = getAgencyProxy( admin );
				// 机构ID
				this.appendToBuffer( buffer, agencyProxy[ 0 ] );
				
				// 代理商ID
				this.appendToBuffer( buffer, agencyProxy[ 1 ] );
				
				// 发布人用户名
				this.appendToBuffer( buffer, admin.getUsername( ) );
				// 面向用户组
				this.appendToBuffer( buffer, bean.getOrientedGroup( ) );
				// 标题
				this.appendToBuffer( buffer, this.cleanHtml( bean.getTitle( ) ) ); // 标题为80个字符，所以这里不设置
				// 内容
				this.appendToBuffer( buffer, this.subString( this.cleanHtml( bean.getContent( ) ) ) ); 
				// 时间
				this.appendToBuffer( buffer, DateUtils.getStringOnFormat( bean.getAddDate( ), 1 ) );
				// 链接地址
				this.appendToBuffer( buffer, "/notice/view.jhtml?id=" + bean.getId( ) );
				
				return buffer.toString( );
				
			}
		};
		
		log.info( "将要发送的数据" + key.value( ) );
		
		
		post.setKeyVal( key );
		task.addTask( post );
		
		send( admin, bean );
	}
	
	private void send( AgentAndAdmins admin, Notice bean ){
		
		Boolean immediately = bean.getImmediately( );
		
		// 如果没有勾选立即发送短信，则不发送短信
		if( immediately == null || !immediately ){
			return;
		}

		boolean validate = validateSmsSend( bean.getSmsSendGroup( ), bean.getSmsContent( ) );
		
		if( !validate ){
			return;
		}
		
		// 发送短信
		smsPost.setAdmin( admin );
		smsPost.setContent( clearHtml( bean.getSmsContent( ) ) );
		smsPost.setSmsGroups( bean.getSmsSendGroup( ) );
		task.addTask( smsPost );
	}
}
